package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ProjectLedgerListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.entity.ProjectLedger;
import com.isunland.managesystem.entity.ProjectLedgerOriginal;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/5/15 13:35
 */
public class ProjectLedgerListFragment extends BaseListFragment {

  private ArrayList<ProjectLedger> mList;
  private int REQUEST_DETAIL = 0;
  private int REQUEST_QUERY = 1;
  private String beginprojectSignDate;//开始时间
  private String endprojectSignDate;//结束时间
  private String projectKindCode;//项目类型code
  private String projectKindName;//项目类型name
  private String projectName;//项目名称
  private String partbDeptName;//往来单位
  private ProjectLedgerListAdapter madapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom("项目台账列表");
    setIsPaging(true);//分页
    mList = new ArrayList<>();
    beginprojectSignDate = MyDateUtil.format(MyDateUtil.getBeforeYear(1), MyDateUtil.TYPE_DATE);
    endprojectSignDate = MyDateUtil.format(new Date(), MyDateUtil.TYPE_DATE);
    projectKindCode = "";
    projectKindName = "";
    projectName = "";
    partbDeptName = "";
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_add_search, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_add:
        //添加
        Intent intent = new Intent(getActivity(), ProjectLedgerAddActivity.class);
        startActivityForResult(intent, REQUEST_DETAIL);
        return true;
      case R.id.menu_item_querys:
        //查询
        ExtraValueContent content = new ExtraValueContent();
        content.setExtName1(beginprojectSignDate);
        content.setExtName2(endprojectSignDate);
        content.setExtName3(projectName);
        content.setExtName4(projectKindName);
        content.setExtCode4(projectKindCode);
        content.setExtName5(partbDeptName);

        Intent query = new Intent(getActivity(), ProjectLedgerQueryActivity.class);
        query.putExtra(ProjectLedgerQueryFragment.EXTRA_VLAUE, content);
        startActivityForResult(query, REQUEST_QUERY);

        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.ULR_PROJECT_LEDGER_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("ifBegin", "F");
    params.put("beginprojectSignDate", beginprojectSignDate);//开始时间
    params.put("endprojectSignDate", endprojectSignDate);//结束时间
    params.put("projectKindCode", projectKindCode);//项目类型编码
    params.put("projectName", projectName);//项目名称
    //params.put("partbDeptName", partbDeptName);//往来单位
    params.put("partaName", partbDeptName);//往来单位
    params.put("orderField", "project_Sign_Date");
    params.put("orderSeq", "desc");
    params.put("type", "mobile");
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    ProjectLedgerOriginal original = new Gson().fromJson(json, ProjectLedgerOriginal.class);
    if (original != null && Base.SUCCESS == original.getResult()) {
      if (getCurrentPage() == 1) {
        mList.clear();
      }
      mList.addAll(original.getRows());
      if (madapter == null) {
        madapter = new ProjectLedgerListAdapter(mActivity, mList);
        setListAdapter(madapter);
      } else {
        madapter.notifyDataSetChanged();
      }
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    ProjectLedger content = mList.get(position - 1);
    Intent intent = new Intent(getActivity(), ProjectLedgerPagerActivity.class);
    intent.putExtra(ProjectLedgerPagerActivity.EXTRA_CONTENT, content);
    startActivityForResult(intent, REQUEST_DETAIL);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == REQUEST_QUERY) {
      ExtraValueContent content =
          (ExtraValueContent) data.getSerializableExtra(ProjectLedgerQueryFragment.EXTRA_VLAUE);
      beginprojectSignDate = content.getExtName1();
      endprojectSignDate = content.getExtName2();
      projectName = content.getExtName3();
      projectKindCode = content.getExtCode4();
      projectKindName = content.getExtName4();
      partbDeptName = content.getExtName5();
    }
    refreshFromTop();//从顶部刷新
    volleyPost();
    super.onActivityResult(requestCode, resultCode, data);
  }
}
